package com.pignest.hotel.beans;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;

import com.pignest.framework.ajax.AjaxObject;
import com.pignest.hotel.annotation.Bean;
import com.pignest.hotel.beans.global.BaseBean;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.DateUtil;

@DataTransferObject
@Bean("会员/员工/房卡")
public class Card extends BaseBean implements AjaxObject {
	private static final long serialVersionUID = -3295101560192441669L;
	/** 卡存储时间格式 */
	public static final String DATE_FORMAT = "yyMMddHHmm";
	
	public static final int TYPE_NORMAL = 0;	//普通卡, 用于制作房卡
	public static final int TYPE_USER = 1;		//员工卡
	public static final int TYPE_MEMBER = 2;	//会员卡
//	public static final int TYPE_ROOM = 3;		//已授权的房卡

	@RemoteProperty
	private int type;
	@RemoteProperty
	private boolean enable;
	@RemoteProperty
	private String cardNumber;
	@RemoteProperty
	private String holderName;
	@RemoteProperty
	private String serialNumber;
	@RemoteProperty
	private String lockNumber;
	private Date expireTime;
	private String remark;
	private Date lastUpdateTime;
	
	public Card() {
	}
	
	public Card(String serialNumber) {
		this.serialNumber = serialNumber;
		this.enable = true;
		this.type = TYPE_NORMAL;
		this.lastUpdateTime = new Date();
	}
	
	public int getType() {
		return type;
	}
	public void setType(int type) {
		this.type = type;
	}
	public boolean isEnable() {
		return enable;
	}
	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	public String getSerialNumber() {
		return serialNumber;
	}
	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}
	public String getLockNumber() {
		return lockNumber;
	}
	public void setLockNumber(String lockNumber) {
		this.lockNumber = lockNumber;
	}
	public Date getExpireTime() {
		return expireTime;
	}
	public void setExpireTime(Date expireTime) {
		this.expireTime = expireTime;
	}
	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}
	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}
	public String getRemark() {
		return remark;
	}
	public void setRemark(String desc) {
		this.remark = desc;
	}
	public String getCardNumber() {
		return cardNumber;
	}
	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}
	public String getHolderName() {
		return holderName;
	}
	public void setHolderName(String holderName) {
		this.holderName = holderName;
	}
	
	/////////////////////////////////////////////////
	
	@RemoteProperty
	public String getRoomName() {
		if (StringUtils.isEmpty(lockNumber))
			return "";
		Room room = Services.getRoomService().getRoomByLock(lockNumber);
		if (room == null)
			return "";
		return room.getName();
	}
	
	public void setExpireTime2(String time) {
		if (time == null || time.length() == 0) {
			expireTime = null;
		} else {
			expireTime = DateUtil.toDate(time, DATE_FORMAT);
		}
	}
	
	public String getExpireTime2() {
		if (expireTime == null)
			return "";
		return DateUtil.toString(expireTime, DATE_FORMAT);
	}
	
	@RemoteProperty
	public String getExpireTime3() {
		if (expireTime == null)
			return "";
		return DateUtil.toString(expireTime, DateUtil.HOTEL_DEFAULT_FORMAT);
	}
	
	/**为写卡服务的日期*/
	public String getExpireTime4() {
		if (expireTime == null)
			return "0000000000";
		return DateUtil.toString(expireTime, "yyMMddHHmm");
	}
	
	@RemoteProperty
	public String getType2() {
		switch (type) {
		case 0:
			return "普通卡";
		case 1:
			return "员工卡";
		case 2:
			return "会员卡";
//		case 3:
//			return "房卡";
		default:
			break;
		}
		return "无效卡";
	}
	
	public String getEnable2() {
		return enable ? "启用" : "禁用";
	}
	
	public boolean isUsable() {
		return !isInUse() && StringUtils.isNotEmpty(cardNumber);
	}
	
	public boolean isInUse() {
		return type == TYPE_USER || type == TYPE_MEMBER || (lockNumber != null && expireTime != null);
	}
	
	public String getHolderName2() {
		if (type == 1)
			return "员工:" + holderName;
		if (type == 2)
			return "会员:" + holderName;
		if (type == 0 && lockNumber != null)
			return "房间:" + holderName;
		return null;
	}
	
	@Override
	public String toSimpleString() {
		return concat(serialNumber, cardNumber, lockNumber, holderName);
	}
}
